home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr09 / pafctool.zip / PAFSUBS.C < prev    next >
C/C++ Source or Header  |  1993-04-19  |  9KB  |  313 lines

  1. /*
  2.  * Copyright (C) 1992, Stephen A. Wood.  All rights reserved.
  3.  *
  4.  * This file is part of PAF C-Tools.
  5.  *  
  6.  * These programs are free software; you can redistribute them and/or modify
  7.  * them under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  * 
  11.  * These programs are distributed in the hope that they will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  * The GNU General Public License can be found in the file LICENSE.
  20.  * 
  21.  * The author of PAF C-Tools is Stephen A. Wood.  He may be reached on internet
  22.  * at the address saw@cegaf.gov, or via mail at 328 Dominion Drive,
  23.  * Newport News, VA 23602.
  24.  * 
  25.  * ------------
  26.  * 
  27.  *This file contains subroutines used by the various PAF C-Tools programs.
  28.  *
  29.  * Last modified:
  30.  *    4/11/92
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include "pafsubs.h"
  35.  
  36. PAF_FILE paf_name = {"name2.dat", 21};
  37. PAF_FILE paf_indiv = {"indiv2.dat", 92};
  38. PAF_FILE paf_marr = {"marr2.dat", 28};
  39. PAF_FILE paf_notes = {"notes2.dat", 256};
  40. PAF_FILE paf_namadd = {"namadd2.dat", 224};
  41.  
  42. void get_paf_rec(RECORD_PTR recno,PAF_FILE *fil,void *buf);
  43. void write_paf_rec(RECORD_PTR recno,PAF_FILE *fil,void *buf);
  44. void paf_open(char c,PAF_FILE *fil);
  45.  
  46. void paf_open_name(char c){paf_open(c,&paf_name);return;}
  47. void paf_open_indiv(char c){paf_open(c,&paf_indiv);return;}
  48. void paf_open_marr(char c){paf_open(c,&paf_marr);return;}
  49. void paf_open_notes(char c)
  50. {
  51.   NOTE_REC pad;
  52.  
  53.   paf_open(c,&paf_notes);
  54.   /* First record is pointer to header of free list */
  55.   /* No notes are ever put in the first record */
  56.   get_note_rec(1,&pad);
  57.   paf_notes.freehead = pad.next_pad;
  58.   paf_notes.freetail = get_tail(paf_notes.freehead,&paf_notes);
  59. /*  fprintf(stderr,"Freehead=%d, Freetail=%d\n",paf_notes.freehead
  60.       ,paf_notes.freetail);
  61. */
  62.   return;
  63. }
  64. void paf_open_namadd(char c){paf_open(c,&paf_namadd);return;}
  65.  
  66.  
  67. void paf_open(char c, PAF_FILE *fil)
  68. {
  69.   char cbuf[11];
  70.   char mode[4];
  71.   
  72.   mode[0] = 'r';
  73.   if(c == 'w'){
  74.     mode[1] = '+';
  75.     mode[2] = 'b';
  76.     mode[3] = 0;
  77.   } else {
  78.     mode[1] = 'b';
  79.     mode[2] = 0;
  80.   }
  81.   if((fil->ptr = fopen(fil->fname,mode)) == NULL){
  82.     fprintf(stderr, "Cannot open %s.\n",fil->fname);
  83.     return;
  84.   }
  85.   (void) fgetc(fil->ptr);
  86.   (void) fread(cbuf,1,10,fil->ptr);
  87.   cbuf[11] = '\0';
  88.   fil->nrec = atoi(cbuf);
  89.   fil->currec = -1;
  90.   if(c=='w'){
  91.     (void) fread(cbuf,1,10,fil->ptr);
  92.     cbuf[11] = '\0';
  93.     fil->freehead = atoi(cbuf);
  94.     fil->freetail = get_tail(fil->freehead,fil);
  95.   }
  96. /*  fprintf(stderr,"%d Records, Freehead=%d, Freetail=%d\n",fil->nrec
  97.       ,fil->freehead,fil->freetail);
  98. */
  99.   return;
  100. }
  101.  
  102.  
  103. PAF_FILE *get_paf_filed(enum paffiles type)
  104. {
  105.   switch(type)
  106.     {
  107.     case NAME:
  108.       return(&paf_name);
  109.     case INDIV:
  110.       return(&paf_indiv);
  111.     case MARR:
  112.       return(&paf_marr);
  113.     case NOTE:
  114.       return(&paf_notes);
  115.     case NAMADD:
  116.       return(&paf_namadd);
  117.     }
  118.   return(0);
  119. }
  120. void get_paf_rec(RECORD_PTR recno,PAF_FILE *fil,void *buf)
  121. {
  122.   long int offset;
  123.     int i;
  124.   
  125.   offset = (long int) recno*fil->reclen;
  126.   (void) fseek(fil->ptr,offset,SEEK_SET);
  127.   i = fread(buf,1,fil->reclen,fil->ptr);
  128.   if(i<=0)printf("Error in read %d\n",recno);
  129.   fil->currec = recno + 1;
  130.   return;
  131. }
  132.   
  133. void get_name_rec(RECORD_PTR recno,NAME_REC *namrec)
  134. {
  135.   char namraw[21];
  136.  
  137.   get_paf_rec(recno,&paf_name,namraw);
  138.   memcpy(&namrec->left,namraw,2);
  139.   memcpy(namrec->name,&namraw[2],17);
  140.   memcpy(&namrec->right,&namraw[19],2);
  141. }
  142. void get_indiv_rec(unsigned short int rin,INDIV_REC *indrec)
  143. {
  144.   char indraw[92];
  145.  
  146.   get_paf_rec(rin,&paf_indiv,indraw);
  147.   memcpy(&indrec->names[0],&indraw[0],10);
  148.   indrec->sex = indraw[10];
  149.   memcpy(&indrec->birthdate,&indraw[11],48);
  150.   memcpy(indrec->ldsinfo,&indraw[59],15); /* Just save LDS, but don't parse */
  151.   memcpy(&indrec->oldersib,&indraw[74],18);
  152. }
  153. void get_marr_rec(unsigned short int mrin,MARR_REC *marec)
  154. {
  155.   char marraw[28];
  156.  
  157.   get_paf_rec(mrin,&paf_marr,marraw);
  158.   memcpy(&marec->husband,&marraw[0],6);
  159.   memcpy(&marec->marriagedate,&marraw[6],17);
  160.   memcpy(&marec->mnext_husband,&marraw[23],4);
  161.   marec->divorce = marraw[27];
  162. }
  163. void get_note_rec(unsigned short int nbp,NOTE_REC *noterec)
  164. {
  165.   char notearray[256];
  166.  
  167.   get_paf_rec(nbp,&paf_notes,notearray);
  168.   memcpy(¬erec->next_pad,notearray,2);
  169.   memcpy(noterec->notelines,¬earray[2],254);
  170. }
  171. void get_namadd_rec(RECORD_PTR recno, NAMADD_REC *namaddrec)
  172. {
  173.   char raw[224];
  174.  
  175.   get_paf_rec(recno,&paf_namadd,raw);
  176.   memcpy(namaddrec->name,raw,41);
  177.   memcpy(namaddrec->addr1,&raw[41],41);
  178.   memcpy(namaddrec->addr2,&raw[82],41);
  179.   memcpy(namaddrec->addr3,&raw[123],41);
  180.   memcpy(namaddrec->phone,&raw[164],26);
  181.   memcpy(namaddrec->stake,&raw[190],26);
  182.   memcpy(namaddrec->unit,&raw[216],8);
  183.   return;
  184. }
  185. void write_paf_rec(RECORD_PTR recno,PAF_FILE *fil,void *buf)
  186. {
  187.   long int offset;
  188.   
  189.   offset = (long int) recno*fil->reclen;
  190.   (void) fseek(fil->ptr,offset,SEEK_SET);
  191.   (void) fwrite(buf,1,fil->reclen,fil->ptr);
  192.   fil->currec = recno + 1;
  193.   return;
  194. }
  195. void write_indiv_rec(unsigned short int rin,INDIV_REC *indrec)
  196. {
  197.   char indraw[92];
  198.  
  199.   memcpy(&indraw[0],&indrec->names[0],10);
  200.   indraw[10] = indrec->sex;
  201.   memcpy(&indraw[11],&indrec->birthdate,48);
  202.   memcpy(&indraw[59],indrec->ldsinfo,15);
  203.   memcpy(&indraw[74],&indrec->oldersib,18);
  204.   write_paf_rec(rin,&paf_indiv,indraw);
  205. }
  206.  
  207. void add_to_freelist(RECORD_PTR newfree, PAF_FILE *fil)
  208. {
  209.   long int offset;
  210.   RECORD_PTR i;
  211.  
  212.   if(fil->freetail !=0) {
  213.     offset = (long int) fil->freetail*fil->reclen;
  214.     (void) fseek(fil->ptr,offset,SEEK_SET);
  215.     (void) fread(&i,1,2,fil->ptr);
  216.     if(i != 0) {printf("TROUBLE\n");exit(1);}
  217.     (void) fseek(fil->ptr,offset,SEEK_SET);
  218.     (void) fwrite(&newfree,1,2,fil->ptr);
  219.   } else {
  220.     fil->freehead = newfree;
  221. /* Assume it is a notes  file  here */
  222.     {
  223.       long int off;
  224.       off = fil->reclen;
  225.       (void) fseek(fil->ptr,off,SEEK_SET);
  226.       (void) fwrite(&newfree,1,2,fil->ptr);
  227.     }
  228.   }
  229.   fil->freetail = get_tail(newfree,fil);
  230.  
  231. }
  232. RECORD_PTR get_tail(RECORD_PTR first, PAF_FILE *fil)
  233. {
  234.   RECORD_PTR next,last;
  235.   long int offset;
  236.  
  237.   next = first;
  238.   last = 0;
  239.   while(next != 0){
  240.     last = next;
  241.     offset = (long int) next*fil->reclen;
  242.     (void) fseek(fil->ptr,offset,SEEK_SET);
  243.     (void) fread(&next,1,2,fil->ptr);
  244.   }
  245.   return(last);
  246. }
  247.  
  248. RECORD_PTR get_next(RECORD_PTR recno, PAF_FILE *fil)
  249. {
  250.   RECORD_PTR next;
  251.   long int offset;
  252.  
  253.   offset = (long int) recno*fil->reclen;
  254.   (void) fseek(fil->ptr,offset,SEEK_SET);
  255.   (void) fread(&next,1,2,fil->ptr);
  256.   return(next);
  257. }
  258.  
  259. void write_note_rec(unsigned short int nbp,NOTE_REC *noterec)
  260. {
  261.   char notearray[256];
  262.  
  263.   memcpy(notearray,¬erec->next_pad,2);
  264.   memcpy(¬earray[2],noterec->notelines,254);
  265.   write_paf_rec(nbp,&paf_notes,notearray);
  266. }
  267.  
  268.  
  269. /*
  270.  * Return pointer to the first record on the free list
  271.  * Reset the free list pointer to point to the next.
  272.  * Only works for notes files.
  273.  */
  274. RECORD_PTR get_free_rec(PAF_FILE *fil)
  275. {
  276.   long int offset;
  277.   RECORD_PTR next,freerec;
  278.   char rec_count[11];
  279.   
  280.   if((freerec = fil->freehead) != 0){
  281.     offset = (long int) freerec*fil->reclen;
  282.     (void) fseek(fil->ptr,offset,SEEK_SET);
  283.     (void) fread(&next,1,2,fil->ptr);
  284.     fil->freehead = next;
  285. /* Assume it is a notes  file  here */
  286.     {
  287.       long int off;
  288.       off = fil->reclen;
  289.       (void) fseek(fil->ptr,off,SEEK_SET);
  290.       (void) fwrite(&next,1,2,fil->ptr);
  291.     }
  292.     if(next == 0) fil->freetail = 0;
  293.     else {
  294.       next = 0;
  295.       (void) fseek(fil->ptr,offset,SEEK_SET);
  296.       (void) fwrite(&next,1,2,fil->ptr);
  297.     }
  298.   }else{    /* No free records, add one to end of file. */
  299.     freerec = ++fil->nrec;
  300.     offset = (long int) freerec*fil->reclen;
  301.     next = 0;
  302.     (void) fseek(fil->ptr,(long int) 0,SEEK_SET);
  303.     sprintf(rec_count," %-9d",fil->nrec);
  304.     printf("Change rec_count to %d\n",fil->nrec);
  305.     if(!fwrite(rec_count,1,10,fil->ptr))
  306.         printf("Error change rec_count to %d\n",fil->nrec);
  307.     (void) fseek(fil->ptr,offset,SEEK_SET);
  308.     (void) fwrite(&next,1,2,fil->ptr); /* Zero out pointer just in case */
  309.   }
  310.   return(freerec);
  311. }
  312.     
  313.